<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Request;
use app\index\model\Course;
use app\index\model\KnowUserLog;
// use app\index\model\KnowUserType;
use app\index\model\KnowUserRecord;



class Record  extends Common
{
	public function add(Request $request)
	{
		$param = $request->param();
		if(user()){
			$user = user();
			$logs = KnowUserRecord::getDetail($param);
			if($param['time'] >= 1 && $param['time'] && $param['time'] != $logs['record']){
				$time = date('Y-m-d H:i:s',time());
				$status = $param['time']/$param['total'] >= 0.9 ? 1 : 0;
				$arr = array(
					'record' => $param['time'],
					'updated_time' => $time
				);
				echo $status;
				$param['status'] = $status;
				$data = Course::getDataOne($param['id']);
				if($logs){
					KnowUserRecord::where(['user_id'=>$user['id'],'course_id'=>$param['id']])->update($arr);
					$param['points'] = $data['points'];
					KnowUserLog::updateData($param);
				}else{
					$arr['user_id'] = $user['id'];
					$arr['course_id'] = $param['id'];
					$arr['created_time'] = $time;
					KnowUserRecord::insert($arr);
					$param['points'] = $param['origin_points'] = $data['points'];
					KnowUserLog::addData($param);
				}
			}
		}
	}

	public function end(Request $request)
	{
		$param = $request->param();
		if(user()){
			$user = user();
			$data = Course::getDataOne($param['id']);
			$record = KnowUserRecord::getDetail($param);
			if ($record['record'] == floor($param['time'])){
				$logs = KnowUserLog::getUserDataOne($param);
				if ($logs){
					$param['status'] = 1;
					$param['points'] = $data['points'];
					KnowUserLog::updateData($param);
				}else{
					$param['status'] = 1;
					$param['points'] = $param['origin_points'] = $data['points'];
					KnowUserLog::addData($param);
				}
				KnowUserRecord::where(['user_id'=>$user['id'],'course_id'=>$param['id']])->update(['status'=>1]);
				/*$logIds = KnowUserLog::getUserCount($param, 'column', 'course.course_id');
				$courseIds = Course::getCourseIds($data['type_id'], 'course.type_id');
				if (asort($logIds) == asort($courseIds)){

				}*/
			}
		}
	}
}